// app.js
App({
  onLaunch() {
    this.globalData.login_token = wx.getStorageSync('login_token');
  },
   // 请求方法封装
  http(type,url,obj,callback) {
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    let that = this
    let param=obj;
    let login_status = this.globalData.login_status;
    if(login_status){
      param.login_token = wx.getStorageSync('login_token');
    }
    wx.request({
      method: type,
      dataType: 'json',
      url: that.globalData.requestUrl + url,
      data: param,
      header: {
        'content-type': 'application/json',
      },
      success: function (res) {
        if(res.data.code==0){
          that.to_error_();return;
        }
        if(res.data.code==2){
          that.to_login_();return;
        }
        if(res.data.code==3){
          that.to_auth_();return;
        }
        if(res.data.code==4){
          that.to_userinfo_();return;
        }
        if(res.data.code==5){
          that.to_last_();return;
        }
        if(res.data.code==6){
          that.ischecked_();return;
        }
        callback(res.data);
        wx.hideLoading();
      },
      complete: function() {
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
  },
  //错误提示
  to_error_:function(msg){
    let that = this;
    wx.showModal({
      title: '提示',
      content: msg,
      confirmText:'确定',
      success (res) {
        
      }
    })
    return ;
  },
  //去登录
  to_login_:function(){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '请先登录',
      confirmText:'去登录',
      success (res) {
        if (res.confirm) {
          that.jump('../login/login');
        }else{
          that.goback();
        }
      }
    })
    return ;
  },
  //去完善个人资料
  to_userinfo_:function(){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '请完善个人资料',
      confirmText:'去完善',
      success (res) {
        if (res.confirm) {
          that.jump('../userinfo/userinfo');
        }else{
          that.goback();
        }
      }
    })
    return;
  },
  //去完善认证资料
  to_auth_:function(){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '请完善认证信息',
      confirmText:'去完善',
      success (res) {
        if (res.confirm) {
          that.jump('../idcard_up/idcard_up');
        }else{
          that.goback();
        }
      }
    })
    return;
  },
  //资料审核中
  to_last_:function(){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '个人信息审核中',
      confirmText:'确定',
      success (res) {
        if (res.confirm) {
          that.goback();
        }else{
          that.goback();
        }
      }
    })
    return;
  },
  //认证已通过
  ischecked_:function(){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '认证已通过',
      confirmText:'确定',
      success (res) {
        if (res.confirm) {
          that.goback();
        }else{
          that.goback();
        }
      }
    })
    return;
  },
  //页面跳转
  jump(url){
    wx.navigateTo({
      url: url
    })
  },
  //回到上页
  goback(){
    wx.navigateBack({
      delta: 1
    })
  },
 
  globalData: {
    userInfo: null,//个人信息
    requestUrl:'https://shop.rongyiht.com/',//接口域名
    login_token:'',//登录令牌
    login_status:false,//登录状态
    auth_status:false,//认证资料是否完善
    user_info_status:false,//个人资料是否完善
  },
  
})
